import pidStore from "../utils/pidStore";

export const login = async (userInfo) => {

	console.log('dologin');
	// #ifdef MP-WEIXIN
	return await new Promise(function(resolve, reject) {
		uni.showLoading({
			title: '加载中'
		});
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function(loginRes) {
				// console.log(loginRes.code);
				let data = {
					code: loginRes.code,
					userInfo: [JSON.stringify(userInfo)],
				};
				if (pidStore.get()) {
					data.pid = pidStore.get()
				}
				uni.request({
					method: 'POST',
					url: 'https://zm.quya.vip/api/v1/miniAuth',
					data,
					success: (res) => {
						if (res.data.data.token) {
							let token = res.data.data.token;
							uni.setStorageSync('token',
								token);

							console.log(uni.getStorageSync(
								'token'));
							uni.hideLoading();
							resolve(token);
						}
					}
				});

			}
		});
	})
	// #endif
}